module user_ui


define page signin() 
{main(signinwiki){

  form {
    table {
      var name : String
      var pass : Secret
      row {column{"Username: "} column{input(name)}}
      row {column{"Password: "} column{input(pass)}}
      row {column {action("Sign in", signin())} column{}}
    }
    	
    action signin() {
      for(u : User) {
        if((u.name == name) && (u.pass.check(pass))) {
          securityContext.principal := u;
          securityContext.loggedIn := true;
          return root();
        }
      }
      message("invalid username/password.");
    }    
  }
  "No account yet? " navigate(register()){"Register"}
  
}}

define page register()
{main(registerwiki){

  form {
    var u := User{}
    table {
      row {column{"Real name: "} column{ input(u.realname)} }
      row {column{"Username: "} column{input(u.name)} }
      row {column{"Password: "} column{input(u.pass)} }
      row {column{action("Register", register())} column{}}
    }
    action register() {
      u.pass := u.pass.digest();
      u.save();
      return signin();
    }
  }
  
}}
// Upon entering the logout page, the securityContext session entity gets cleared
define page signoff() 
{main(signoffwiki){

  init {
    securityContext.principal := null;
    securityContext.loggedIn := false;
    return root();
  }
  
}}

// homepage(u : User): Display user's homepage
define page homepage(u: User, popsort: Bool, bfilt: String)
{main(homepagewiki){

  var bmentrylist: List<BookmarkEntry>
  init {
    if(popsort) {
      bmentrylist := [bmentry | 
      	bmentry: BookmarkEntry in u.bmentries order by bmentry.popularity desc];
    } else {
      bmentrylist := [bmentry | 
      	bmentry: BookmarkEntry in u.bmentries order by bmentry.date desc];
    } 
  }
   
  table {
    row {
      // bookmark entries column
      column[id:=bookmarks] {
        // display header and list control links
        block[id:=header] {
          form {
            if(popsort) {
              "Most popular "
            } else {
              "Most recent "
            }
            output(u.name)"'s bookmarks"
            if(popsort) {
              actionLink(" (view most recent)", action{return homepage(u, false, bfilt);})
            } else {
              actionLink(" (view most popular)", action{return homepage(u, true, bfilt);})
            }
            " "
            input(bfilt)
            actionLink("filter", action{return homepage(u, popsort, bfilt);})
          }
        }
        // display list items
        list {
          for(bmentry: BookmarkEntry in bmentrylist where bmentry.matches(bfilt)) {
            listitem {
              listbookmark(bmentry.bm, bmentry)
            }
          }
        }
      }
      // tag entries column
      column[id:=tags] {
        // header
        output(u.name)"'s Tags"
        // list tagentries
        list {
          for(tgentry: TagEntry in u.tgentries order by tgentry.popularity desc) {
            listitem {
              listtag(tgentry.tag, tgentry)
            }
          }
        }
      }
    }
  }

}}

// Display list of system users. Clicking on a user goes to homepage(u)
define page users()
{main(userswiki){
  list{
    for(u : User) {
      listitem { 
        output(u.realname)
        "(" navigate(homepage(u, false, "")){output(u.name)} ")"
      }
    }
  } 
}}

define page bookmarkentry_create(url : String)
{main(bookmarkentry_createwiki){
  form {
    table {
      init {
        // if entry already exists, go to edit it.
        var bmentry := securityContext.principal.bmentries_find(url);
        if(bmentry != null) {
          return bookmarkentry_edit(bmentry);
        }
      }
      var title: String
      // TODO: implement suggestions for tags
      var tagnames: String
      row {column{"URL: "} column{input(url)}}
      row {column{"Title: "} column{input(title)}}
      row {column{"Tags: "} column{input(tagnames)}}
      row {column{action("Save", save()) action("Cancel", action{return root();})}}
      action save() {
        // if a bookmark already exists with this url, do not create new
        var bm := findBookmark(url);
        if(bm == null) {
          bm := Bookmark{url:=url as URL, date:=now(), popularity:=0};
          bm.save();
        }
        securityContext.principal.bmentries_add(title, bm, tagnames);
        return root();
      }
    }
  }
}}



// Display all bookmark entries associating to this tag entry
define page tagentry_references(tgentry: TagEntry)
{main(tagentry_referenceswiki){
  par { "Bookmark entries of " output(tgentry.user.name) " for " output(tgentry.tag.name) }
  list {
    for(bmentry: BookmarkEntry in tgentry.bmentries order by bmentry.popularity desc) {
      listitem {
        listbookmark(bmentry.bm, bmentry)
      }
    }
  }
}}



define page bookmarkentry_edit(bmentry : BookmarkEntry)
{main(bookmarkentry_editwiki){
  form {
    var title := bmentry.title
    var tagnames: String := securityContext.principal.gettagnames(bmentry)
    table {
      row { column{"URL: "} column{output(bmentry.bm.url)} }
      row { column{"Title: "} column{input(title)} }
      row { column{"Tags: "} column{input(tagnames)} }
      row { column{action("Save", save()) action("Cancel", cancel())} column{} }
    }
    action save() {
      securityContext.principal.bmentries_edit(bmentry, title, tagnames);
      return root();
    }
    action cancel() {
      return root();
    }
  }
}}
